/**
 *Copyright (c) [Year] [name of copyright holder]
 * [Software Name] is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.workflow.api.activiti;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.IdentityService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.ResourceUtils;

@RunWith(SpringRunner.class)
@SpringBootTest
public class ParallelGatWay {

	@Autowired
	private RepositoryService repositoryService;

	@Autowired
	private TaskService taskService;

	@Autowired
	private RuntimeService runtimeService;

	@Autowired
	private IdentityService identityService;
	
	@Test
	public void deployment() throws Exception {
		File file = new File(ResourceUtils.getURL("classpath:process/ParallelGatway.bpmn").getPath());
		InputStream in = new FileInputStream(file);
		Deployment deployment = repositoryService.createDeployment().addInputStream(file.getAbsolutePath(), in).deploy();
		Assert.assertNotNull(deployment);
	}

	@Test
	public void start() throws Exception {
		try {
			Map<String, Object> variables = new HashMap<String, Object>();
			variables.put("transition", "同意");
			variables.put("count1", 3);
			variables.put("userList1", Arrays.asList("admin1", "admin2", "admin3"));
			
			variables.put("count2", 3);
			variables.put("userList2", Arrays.asList("admin1", "admin2", "admin3"));
			
			variables.put("count3", 3);
			variables.put("userList3", Arrays.asList("admin1", "admin2", "admin3"));
			
			identityService.setAuthenticatedUserId("admin");
			ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("parallelGatWay", "1008688", variables);
			Assert.assertNotNull(processInstance);
			System.out.println("processInstance id ---> " + processInstance.getId());
			
			Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
			taskService.claim(task.getId(), "admin");
			taskService.complete(task.getId(), variables);
			
		} finally {
			identityService.setAuthenticatedUserId(null);
		}
	}
	
	@Test
	public void commit() throws Exception {
		try {
			List<Task> tasks = taskService.createTaskQuery().processInstanceId("100001").list();
			for(Task task : tasks) {
				Map<String, Object> variables = new HashMap<String, Object>();
				variables.put("transition", "通过");
				variables.put("taskId", "57516");
				variables.put("operator", "admin");
				taskService.claim(task.getId(), "admin");
				taskService.complete(task.getId(), variables);
			}
			Assert.assertNotNull(tasks);
			Assert.assertEquals(1, tasks.size());
		} finally {
			identityService.setAuthenticatedUserId(null);
		}
	}
	
}
